import json
from newpro.db import db
from newpro.views.base import BaseHandler

class ResourceHandler(BaseHandler):
    def get(self):
        roleid = self.get_query_argument("roleid")
        sql = "select * from resource where pid>0"
        res = db.findAll(sql)
        data = [{"key": i['id'], "label": i['name']} for i in res]
        sql1 = "select resourceid from role_resource where userid=%d" % (int(roleid))
        idlist = db.findAll(sql1)
        checklist = []
        for j in idlist:
            checklist.append(j['resourceid'])
        mes = {"code": 200, "reslist": data, "checklist": checklist}
        self.write(mes)
    def post(self):
        data = self.request.body
        mes = json.loads(data)
        roleid = mes['roleid']
        value = mes['resid']
        db.add("delete from role_resource where userid = %d" % int(roleid))
        db.commit()
        for i in value:
            sql = "insert into role_resource(userid, resourceid) values (%d,%d)"%(int(roleid),int(i))
            db.add(sql)
            db.commit()
        self.write({"code": 200, "msg": "ok"})
class LeftmenuHandler(BaseHandler):
    def get(self):
        userid = self.get_query_argument('roleid')
        sql = "select re.*,pre.name as pname from role_resource as rr left join resource as re on rr.resourceid=re.id left join resource as pre on pre.id = re.pid where rr.userid=%d"%int(userid)
        res = db.findAll(sql)
        list = []
        menulist = []
        for i in res:
            if i['pid'] not in menulist:
                list.append({'id': i['pid'], 'name': i['pname'], 'son': []})
                menulist.append(i['pid'])
        for j in res:
            for index, p in enumerate(list):
                if j['pid'] == p['id']:
                    list[index]['son'].append({'id': j['pid'], 'name': j['name'], 'url': j['url']})
        self.write({'code': 200, 'list': list})

class OneSort(BaseHandler):
    def get(self):
        sql = "select * from resource where pid=0"
        res = db.findAll(sql)
        self.write({'code':200,'list':res})

# 添加资源
class Addrescource(BaseHandler):
    def post(self):
        data = self.request.body
        mes = json.loads(data)
        name = mes['name']
        url=mes['url']

        pid = mes['pid']
        sql = "insert into resource(name, url, pid) values ('%s','%s',%d)"%(name,url,int(pid))
        db.add(sql)
        db.commit()
        self.write({'code':200,'msg':'ok'})



#获取所有的pagesource
class Getpagesource(BaseHandler):
    def get(self):
        sql = "select * from pagesource"
        res = db.findAll(sql)
        self.finish({'code':200,'data':res})
class Getresourcepid1(BaseHandler):
    def get(self):
        sql = "select * from resource where type=1 and pid !=0"
        res = db.findAll(sql)
        self.finish({'code':200,'data':res})

class Postpagesource(BaseHandler):
    def post(self):
        data = json.loads(self.request.body)
        roleid = data['roleid']
        resourceid = data['resourceid']
        pageid = data['pageid']
        for i in pageid:
            sql = "insert into role_resource(roleid, resourceid, pageid) values (%d,%d,%d)"%(int(roleid),int(resourceid),int(i))
            db.update(sql)
            db.commit()
        return self.finish({'code':200,'msg':'ok'})

class Getpageource(BaseHandler):
    def get(self):
        roleid = self.get_query_argument('roleid')
        url = self.get_query_argument('url')
        sql1 = "select id from resource where url='%s' "%url
        mes = db.findone(sql1)
        resourceid = mes['id']
        # print('1278371287',resourceid)
        sql = "select pageid from role_resource where roleid=%d and resourceid=%d and  pageid is not null "%(int(roleid),int(resourceid))
        res = db.findall(sql)
        print(res)
        self.finish({'code':200,'data':res})


